package com.serge.smartdacha;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsMessage;
import android.util.Log;
import com.serge.smartdacha.model.PerimeterController;
import com.serge.smartdacha.model.SmsParser;

public class SmartReceiver extends BroadcastReceiver {

    private final String TAG = this.getClass().getSimpleName();

    public static final int SMS_NOTIFICATION_ID = 1;


    @Override
    public void onReceive(Context context, Intent intent) {

        Log.i(TAG, "SmartReceiver.onReceive");

        Bundle extras = intent.getExtras();

        if (extras != null) {
            Object[] smsextras = (Object[]) extras.get("pdus");

            SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
            String smart_dacha_phone = sharedPrefs.getString(SettingsActivity.PREF_PHONE_NUMBER, "");
            boolean abortBroadcast = sharedPrefs.getBoolean(SettingsActivity.PREF_ABORT_BROADCAST, true);

            for (Object smsextra : smsextras) {
                SmsMessage smsmsg = SmsMessage.createFromPdu((byte[]) smsextra);

                String messageBody = smsmsg.getMessageBody();
                String sender = smsmsg.getOriginatingAddress();

                if (smart_dacha_phone.equals(sender)) {

                    SharedPreferences.Editor editor = sharedPrefs.edit();
                    editor.putString(MainActivity.PREF_LATEST_SMS_BODY, messageBody);
                    editor.putLong(MainActivity.PREF_LATEST_SMS_TS, System.currentTimeMillis());
                    editor.commit();

                    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE);

                    Resources resources = context.getResources();
                    Notification.Builder builder = new Notification.Builder(context)
                            .setDefaults(Notification.DEFAULT_ALL)
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setAutoCancel(true)
                            .setContentTitle(resources.getString(R.string.sms_notification_title));

                    PerimeterController perimeterController = new SmsParser().extract(messageBody, PerimeterController.class);
                    if (perimeterController != null && perimeterController.getAlarm()) {
                        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
                        builder.setSound(notification, AudioManager.STREAM_ALARM);
                        builder.setLights(Color.RED, 500, 500);
                        builder.setContentText(resources.getString(R.string.sms_notification_alarm));
                    } else {
                        builder.setContentText(resources.getString(R.string.sms_notification_text));
                    }


                    Intent i1 = new Intent(context, MainActivity.class);
                    i1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    builder.setContentIntent(PendingIntent.getActivity(context, 0, i1, PendingIntent.FLAG_CANCEL_CURRENT));
                    notificationManager.notify(SMS_NOTIFICATION_ID, builder.getNotification());

                    context.sendBroadcast(new Intent(MonitorFragment.LOAD_DATE_ACTION));

                    if(abortBroadcast) {
                        this.abortBroadcast();
                    }
                }
            }

        }

    }


}